/*    */ package servlet;

/*    */
/*    */ import com.google.gson.Gson;
/*    */ import enums.CommonEnums;
/*    */ import form.RecruitForm;
/*    */ import java.io.IOException;
/*    */ import javax.servlet.ServletException;
/*    */ import javax.servlet.annotation.WebServlet;
/*    */ import javax.servlet.http.HttpServlet;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */ import service.Impl.RecruitServiceImpl;
/*    */ import service.RecruitService;
/*    */ import util.JsonUtil;
/*    */ import util.ResultVOUtil;

/*    */
/*    */
/*    */ @WebServlet(name = "add", urlPatterns = { "/add" }, asyncSupported = true)
/*    */ public class addServlet/*    */ extends HttpServlet
/*    */ {
	/* 22 */ private RecruitService recruitServiceImpl = new RecruitServiceImpl();
	/*    */

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/*    */ protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/* 25 */ resp.setCharacterEncoding("utf-8");
		/* 26 */ String json = JsonUtil.getStrResponse(req);
		/* 27 */ RecruitForm recruitForm = (RecruitForm) (new Gson()).fromJson(json, RecruitForm.class);
		/* 28 */ String remoteIp = getIpAddr(req);

		/* 29 */ recruitForm.setIp(remoteIp);
		/* 30 */ recruitForm.setType("e");
		System.out.println("json:" + json);
		/* 31 */ if (recruitForm.getMajor().equals("计算机科学与技术")) {
			/* 32 */ recruitForm.setType("a");
			/*    */ }
		/* 34 */ if (recruitForm.getMajor().equals("物联网")) {
			/* 35 */ recruitForm.setType("b");
			/*    */ }
		/* 37 */ if (recruitForm.getMajor().equals("软件工程")) {
			/* 38 */ recruitForm.setType("c");
			/*    */ }
		/* 40 */ if (recruitForm.getMajor().equals("信息安全")) {
			/* 41 */ recruitForm.setType("d");
			/*    */ }
		/* 43 */ System.out.println("remote:" + remoteIp);
		/* 44 */ resp.setContentType("text/html");
		/* 45 */ if (!this.recruitServiceImpl.findByIp(remoteIp)) {
			/* 46 */ if (this.recruitServiceImpl.add(recruitForm)) {
				/* 47 */ resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
				/*    */ } else {
				/* 49 */ resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
				/*    */ }
			/*    */ } else {
			/* 52 */ resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.DOUPLE_SUBMIT)));
			/*    */ }
		/*    */ }
	/*    */ }

/*
 * Location:
 * /Users/frank/Desktop/iotlab/WEB-INF/classes/!/servlet/addServlet.class Java
 * compiler version: 8 (52.0) JD-Core Version: 1.0.7
 */